package metier;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;


import vue.DashBoard;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.widget.Toast;

public class httpTask extends AsyncTask< Object[], Void, String>   
{

	public Context context;
	public Activity act;
	//public class classes;

	 protected String doInBackground(Object[]... arg0) {
		 
		// classes = (String)arg0[0][3];
		 act = (Activity)arg0[0][4];
		 this.setContext((Context)arg0[0][2]);
		 
		 @SuppressWarnings("unchecked")
		List<NameValuePair> ok = (List<NameValuePair>)arg0[0][0];
		 String url  = (String) arg0[0][1];

		 DefaultHttpClientPerso httpclient = new DefaultHttpClientPerso();
		HttpPost httpget = new HttpPost(url);
		
			  try 
			  {
		          httpget.setEntity(new UrlEncodedFormEntity(ok));
		      } catch (UnsupportedEncodingException e) {
		         e.printStackTrace();
		      }

			HttpResponse response = null;
			String responseString = null;
			
			
			 try
			 {
			
				 response = httpclient.execute(httpget);
				 
			 } catch (ClientProtocolException e) {
				 e.printStackTrace();
			 } catch (IOException e) {
				 e.printStackTrace();
			 } 
			    
			 
			 HttpEntity entity = response.getEntity();
			 
			 if ( entity != null )
				 try 
			 	{
					 responseString = EntityUtils.toString(entity);
			 	}
			 catch (ParseException e) 
			 		{
					 e.printStackTrace();
				    } 
			 catch (IOException e) {
				     e.printStackTrace();
				    }
					 
			return responseString;

				 	}

			
		public Context getContext() {
		return context;
	}


	public void setContext(Context con) {
		this.context = con;
	}


		protected void onPostExecute(String result) 
		{
	
			if ( result.startsWith("Vous") )
				
			{
				Toast.makeText(getContext(),"connecté",Toast.LENGTH_LONG).show();	
			}
			
			if ( result.startsWith("Inscription") )
				
			{
				Toast.makeText(getContext(),"Inscription",Toast.LENGTH_LONG).show();
				Intent nextScreen = new  Intent (getContext (), DashBoard.class );
				act.startActivity (nextScreen);
				
				
			}
		}
			

}
		
	


	
	
	
	
	
	
	
	
	
	
	
	
	
	

